/* Copyright (c) 2008 LepomisLab.com, Andrey Turkin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "NSActionCell.h"
#include "NSKeyedUnarchiver.h"
#include "NSControl.h"

NSActionCell::NSActionCell() : NSCell(), _control_view(NULL), _target(NULL), _argument(NULL), _tag(-1)
{
	exposeBinding(wxT("action"));
	exposeBinding(wxT("target"));
	exposeBinding(wxT("argument"));
}

void NSActionCell::initWithUnarchiver(NSKeyedUnarchiver *archiver)
{
	NSCell::initWithUnarchiver(archiver);
	if (archiver->contains(wxT("NSTag")))
		_tag = archiver->decodeIntForKey(wxT("NSTag"));
	if (archiver->contains(wxT("NSTarget")))
		archiver->decodeObjectForKey(wxT("NSTarget"));
	if (archiver->contains(wxT("NSAction")))
		archiver->decodeStringForKey(wxT("NSAction"));
}

void NSActionCell::_setValue(const wxString& key, wxObject *value)
{
	if (key == wxT("action"))
	{
		if (value)
		{
			wxVariant *v = wxDynamicCast(value, wxVariant);
			wxASSERT(v);
			_action = v->GetString();
		}
	} else if (key == wxT("tag"))
	{
		wxVariant *v = wxDynamicCast(value, wxVariant);
		wxASSERT(v);
		_tag = v->GetLong();
	} else if (key == wxT("target"))
	{
		NSObject* t = wxDynamicCast(value, NSObject);
		wxASSERT(t);
		_target = t;
	} else if (key == wxT("argument"))
	{
		_argument = value;
	} else
		NSCell::_setValue(key, value);
}

wxObject* NSActionCell::_getValue(const wxString& key)
{
	if (key == wxT("action"))
	{
		return new wxVariant(_action);
	} else if (key == wxT("target"))
	{
		return _target;
	} else if (key == wxT("tag"))
	{
		return new wxVariant(_tag);
	} else if (key == wxT("argument"))
	{
		return _argument;
	} else
		return NSCell::_getValue(key);
}

void NSActionCell::performClick(void)
{
	NSControl *_view = wxDynamicCast(controlView(), NSControl);
	if (_view)
		_view->sendClickAction();
}

void NSActionCell::updateWidget(void)
{
	if (_widget)
		controlView()->_initWidget(_widget);
}
